﻿using System;
using System.ComponentModel;
using System.Data.Services.Client;
using Coevery.ViewModel.EntityDataService;

namespace Coevery.ViewModel
{
    public class BaseViewModel : INotifyPropertyChanged
    {
        private static CodeFirstContainer _context;

        public event PropertyChangedEventHandler PropertyChanged;

        public static CodeFirstContainer Context
        {
            get
            {
                if(_context == null)
                {
                    _context = new CodeFirstContainer(new Uri("http://api.coevery.com/coevery/EntityDataService.svc"));
                    _context.MergeOption = MergeOption.OverwriteChanges;
                    Context.SendingRequest += SendingRequest;
                }
                return _context;
            }
        }

        public static string Cookie { get; set; }

        private static void SendingRequest(object sender, SendingRequestEventArgs e)
        {
            e.RequestHeaders["Cookie"] = Cookie;
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var propertyChanged = PropertyChanged;
            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
